using System.Collections;
using System.Threading.Tasks;

namespace GameKitUnity.Core
{
    public static class TaskExtensions
    {
        public static Task DelayInMillisecond(this UnityEngine.MonoBehaviour target, float millisecond)
        {
            var tcs = new TaskCompletionSource<bool>();
            target.StartCoroutine(DelayCoroutineInMillisecond(millisecond, tcs));
            return tcs.Task;
        }

        public static Task DelayInSeconds(this UnityEngine.MonoBehaviour target, float seconds)
        {
            var tcs = new TaskCompletionSource<bool>();
            target.StartCoroutine(DelayCoroutineInSeconds(seconds, tcs));
            return tcs.Task;
        }

        private static IEnumerator DelayCoroutineInMillisecond(float millisecond, TaskCompletionSource<bool> tcs)
        {
            yield return new UnityEngine.WaitForSeconds(millisecond / 1000f);
            tcs.SetResult(true);
        }

        private static IEnumerator DelayCoroutineInSeconds(float seconds, TaskCompletionSource<bool> tcs)
        {
            yield return new UnityEngine.WaitForSeconds(seconds);
            tcs.SetResult(true);
        }
    }
}